<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');


class MeuploadController extends JController
{
    /**
     * Method to display the view
     *
     * @access    public
     */
    function display()
    {
        $ac = '';
        switch($this->getTask())
        {
            default:
            {
                JToolBarHelper::deleteList();
                JToolBarHelper::editListX();
                JToolBarHelper::addNewX();
            } break;
            case 'add':
            {
                $post = JRequest::get('post');
                if($post['step']=='2')
                {
                    $this->CreateFolder($post['cid'][0]);
                }

                $ac = '[add]';
                //JRequest::setVar( 'hidemainmenu', 1 );
                JRequest::setVar( 'layout', 'form'  );
                JRequest::setVar( 'view', 'meupload' );
                JRequest::setVar( 'edit', false  );
            } break;
            case 'edit':
            {
                $ac = '[edit]';

                $get = JRequest::get('get');
                if(!empty($get['task']))
                {
                    print_r($get);
                    JRequest::setVar( 'cid[]', $get['cid'][0] );
                }
                //JRequest::setVar( 'hidemainmenu', 1 );
                JRequest::setVar( 'layout', 'form'  );
                JRequest::setVar( 'view', 'meupload' );
                JRequest::setVar( 'step', '2' );
                JRequest::setVar( 'edit', true  );
            } break;
        }
        JToolBarHelper::title( 'Stories Manager'.$ac, 'generic.png' );
        parent::display();
    }


    function cancel()
    {
        $this->setRedirect( 'index.php?option=com_meupload','Operation cancel.' );
    }


    function CreateFolder($name)
    {
        $folder_name = JPATH_SITE.DS.'images'.DS.'stories'.DS.$name;
        if(is_dir($folder_name)===false)
        {
            mkdir($folder_name);
            chmod($folder_name, 0777);
            //$mic = substr(microtime(), 2, 4);
            //$myname = $name.'_'.$mic;
            //@mkdir($folder_name.'_'.$mic, 0777);
            return $name;
        }
        else
        {
            return $name;
        }
    }


    function remove(){
        $post = JRequest::get('post');
        if(!empty($post['del_path']))
        {
            for($i=0; $i<count($post['cid']); $i++){
                $file = $post['cid'][$i];
                chmod($post['del_path'].DS.$file, 0777);
                unlink($post['del_path'].DS.$file);
            }
            $msg = 'Delete files success.';
        }
        else
        {
            $path = JPATH_ROOT.DS.'images'.DS.'stories'.DS.$post['cid'][0];
            $files_list = Folderlist::showfiles($path);
            for($i=0; $i<count($files_list); $i++){
                $file = $files_list[$i];
                chmod($path.DS.$file, 0777);
                unlink($path.DS.$file);
            }
            rmdir($path);
            $msg = 'Delete file image and folder success.';
        }
        $this->setRedirect( 'index.php?option=com_meupload', $msg );
    }
}
?>
